<?php
namespace app\index\controller;


use app\admin\util\Response;
use app\index\model\PicCommentModel;
use app\index\model\PicImgModel;
use app\index\model\PicLikeModel;
use app\index\model\PicModel;
use app\index\model\VideoCommentModel;
use app\index\model\VideoLikeModel;
use app\index\model\VideoModel;
use app\index\util\Page;
use app\index\validate\PicValidate;
use app\index\validate\VideoValidate;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use think\Config;
use think\Db;
use think\Request;
use think\Session;

class Video extends BaseMessage
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->_template_index = 'index';
        $this->_template_ajax_list = 'ajax_list';
        $this->_template_add = 'add';
        $this->_template_detail = 'detail';
        $this->_model = new VideoModel();
        $this->_like_model = new VideoLikeModel();
        $this->_comment_model = new VideoCommentModel();
        $this->_validate = new VideoValidate();
    }
    /**
     * 输出列表页
     * @return \think\response\View
     */
    public function index()
    {
        $order = input('order','1','intval');
        $this->assign('order',$order);
        $data = $this->_get_list($order,$this->_size,1);
        $list = $data['data'];
        $this->assign('list_end',$data['is_end']);
        $this->assign('list',$list);
        return view($this->_template_index);
    }
    /**
     * 获取列表方式
     * @param $order
     * @param $size
     * @param $page
     * @return array
     */
    protected function _get_list($order,$size,$page){
        if($order == 1){
            $list = $this->_model->field('*')->alias('m')->join('(SELECT count(id) as like_num,message_id FROM '.$this->_like_model->getTable().' GROUP BY message_id) AS c',' m.id = c.message_id','LEFT')->where(['m.status' => 1])->order('like_num desc')->select();
        }else if($order == 2){
            $list = $this->_model->field('count(*) as comment_num,m.*')->alias('m')->join($this->_comment_model->getTable().' c ',' m.id = c.message_id','LEFT')->where(['m.status' => 1])->order('m.update_time desc')->group('m.id')->select();
        }elseif($order == 3){
            $list = $this->_model->where(['status' => 1])->order('pv desc')->select();
        }
        foreach ($list as &$value){
            $value['like_num'] = $this->_get_like_num($value['id']);
            $value['comment_num'] = $this->_get_comment_num($value['id']);
        }
        return Page::return_page_data($list,$size,$page);
    }
    public function insert(){
        $param = [];
        $param['title'] = input('title','','htmlspecialchars');
        $param['video'] = input('video','','htmlspecialchars');
        $param['uid'] = $this->_user_id;
        if(Config::get('site.message_default_status') == 1){
            $param['status'] = 1;
        }else{
            $param['status'] = 0;
        }
        $validate = new VideoValidate();
        $result = $validate->check($param);
        if($result === false){
            Response::json(-1,$validate->getError());
        }else{
            $result = $this->_model->isUpdate(false)->save($param);
            if($result){
                Response::json(1,'操作成功');
            }else{
                Response::json(-1,'添加失败');
            }
        }
    }

    public function my_list(){
        $page = input('page',1,'intval');
        $list = $this->_model->where(['uid'=>$this->_user_id])->select();
        foreach ($list as &$value){
            $value['like_num'] = $this->_get_like_num($value['id']);
            $value['comment_num'] = $this->_get_like_num($value['id']);
        }
        $result =  Page::return_page_data($list,$this->_size,$page);
        $this->assign('list',$result['data']);
        if(Request::instance()->isAjax()){
            $content = $this->fetch('ajax_my_list');
            $result['content'] = $content;
            Response::json(1,'加载成功',$result);
        }else{
            $list = $result['data'];
            $this->assign('list_end',$result['is_end']);
            return view();
        }
    }
    public function update(){
        $id = input('id',0,'intval');
        $where = [];
        $where['id'] = $id;
        $where['uid'] = $this->_user_id;
        if(!$this->_model->where($where)->find()){
            Response::json(-1,'您所更新的资料不存在');
        }
        $param = [];
        $param['title'] = input('title','','htmlspecialchars');
        $param['video'] = input('video','','htmlspecialchars');
        if($param['video'] == ''){
            Response::json(-1,'请上传你的视频');
        }
        $result = $this->_validate->check($param);
        if($result === false){
            Response::json(-1,$this->_validate->getError());
        }else{
            $this->_model->save($param,$where);
            if($result){
                Response::json(1,'操作成功');
            }else{
                Response::json(-1,'添加失败');
            }
        }
    }
    protected function _before_add()
    {
        $this->assign('action',url('insert'));
        $id = input('id',0,'intval');
        $this->assign('back_url',url('index'));
        if($id != 0){
            $where = [];
            $where['id'] = $id;
            $where['uid'] = $this->_user_id;
            $vo = $this->_model->where($where)->find();
            if($vo){
                $this->assign('vo',$vo);
                $this->assign('action',url('update'));
            }
            $this->assign('back_url',url('my_list'));
        }
    }
}
